#include "Arduino.h"
#include "gpio.h"

uint8_t EXIT_Num = 0;

// 初始化函数
void GPIO_Init(uint8_t pin, uint8_t mode)
{
    pinMode(pin,mode);
}

// 写电平函数
void GPIO_WritePin(uint8_t pin, uint8_t val)
{
    digitalWrite(pin, val);
}

// 读电平函数
uint8_t GPIO_ReadPin(uint8_t pin)
{
    return digitalRead(pin);
}

// 外部中断初始化化函数
void GPIO_EXIT_Init(uint8_t pin, uint8_t gpiomode ,uint8_t mode)
{
    GPIO_Init(pin,gpiomode);

    if(EXIT_Num == 0)
    {
        attachInterrupt(digitalPinToInterrupt(pin),GPIO_EXIT_Interrupt_0,mode);
    }
    
    if(EXIT_Num == 1)
    {
        attachInterrupt(digitalPinToInterrupt(pin),GPIO_EXIT_Interrupt_1,mode);
    }

    if(EXIT_Num == 2)
    {
        attachInterrupt(digitalPinToInterrupt(pin),GPIO_EXIT_Interrupt_2,mode);
    }

    if(EXIT_Num == 3)
    {
        attachInterrupt(digitalPinToInterrupt(pin),GPIO_EXIT_Interrupt_3,mode);
    }

    if(EXIT_Num == 4)
    {
        attachInterrupt(digitalPinToInterrupt(pin),GPIO_EXIT_Interrupt_4,mode);
    }
    
    if(EXIT_Num == 5)
    {
        attachInterrupt(digitalPinToInterrupt(pin),GPIO_EXIT_Interrupt_5,mode);
    }

    if(EXIT_Num == 6)
    {
        attachInterrupt(digitalPinToInterrupt(pin),GPIO_EXIT_Interrupt_6,mode);
    }

    if(EXIT_Num == 7)
    {
        attachInterrupt(digitalPinToInterrupt(pin),GPIO_EXIT_Interrupt_7,mode);
    }

    if(EXIT_Num == 8)
    {
        attachInterrupt(digitalPinToInterrupt(pin),GPIO_EXIT_Interrupt_8,mode);
    }
    
    if(EXIT_Num == 9)
    {
        attachInterrupt(digitalPinToInterrupt(pin),GPIO_EXIT_Interrupt_9,mode);
    }

    if(EXIT_Num == 10)
    {
        attachInterrupt(digitalPinToInterrupt(pin),GPIO_EXIT_Interrupt_10,mode);
    }

    if(EXIT_Num == 11)
    {
        attachInterrupt(digitalPinToInterrupt(pin),GPIO_EXIT_Interrupt_11,mode);
    }

    EXIT_Num += 1;
}

/*可调用的中断函数，根据中断的配置先后顺序后选用*/

// =========================用作八路循迹模块的外部中断=========================
__weak_symbol void GPIO_EXIT_Interrupt_0(void)  {}
__weak_symbol void GPIO_EXIT_Interrupt_1(void)  {}
__weak_symbol void GPIO_EXIT_Interrupt_2(void)  {}
__weak_symbol void GPIO_EXIT_Interrupt_3(void)  {}
__weak_symbol void GPIO_EXIT_Interrupt_4(void)  {}
__weak_symbol void GPIO_EXIT_Interrupt_5(void)  {}
__weak_symbol void GPIO_EXIT_Interrupt_6(void)  {}
__weak_symbol void GPIO_EXIT_Interrupt_7(void)  {}
// =========================用作八路循迹模块的外部中断=========================

// 其它可调用的外部中断
__weak_symbol void GPIO_EXIT_Interrupt_8(void)  {}
__weak_symbol void GPIO_EXIT_Interrupt_9(void)  {}
__weak_symbol void GPIO_EXIT_Interrupt_10(void) {}
__weak_symbol void GPIO_EXIT_Interrupt_11(void) {}